﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MM.Common.Setup
{
    public partial class FrmMulSelFromGrid : XtraForm
    {
        private SaveData _saveData;
        protected DataTable dtFrom;
        protected DataTable dtTo;
        protected DataTable dtTo_s;

        public FrmMulSelFromGrid(DataTable dtAll, string[] column_name, int[] column_width, string title, ref DataTable dt_choice)
        {
            InitializeComponent();
            this.dtFrom = dtAll.Copy();
            this.dtTo = dt_choice.Copy();
            this.dtTo_s = dt_choice;
            for (int i = 0; i < this.dtTo.Rows.Count; i++)
            {
                DataRow row = this.dtFrom.Rows.Find(this.getPrimaryValue(this.dtTo.Rows[i]));
                if (row != null)
                {
                    this.dtFrom.Rows.Remove(row);
                }
            }
            this.gridControl1.DataSource = this.dtFrom;
            this.gridControl2.DataSource = this.dtTo;
            this.Text = title;
            this.SetColumns(column_name, column_width);
        }

        public FrmMulSelFromGrid(DataTable dtAll, string[] column_name, int[] column_width, string title, ref DataTable dt_choice, SaveData savedata): this(dtAll, column_name, column_width, title, ref dt_choice)
        {
            this._saveData = savedata;
        }

        private void addRow(DataRow dr)
        {
            DataRow row = this.dtTo.NewRow();
            for (int i = 0; i < this.dtFrom.Columns.Count; i++)
            {
                string columnName = this.dtFrom.Columns[i].ColumnName;
                if (this.dtTo.Columns.Contains(columnName))
                {
                    row[columnName] = dr[columnName];
                }
            }
            this.dtTo.Rows.Add(row);
            dr.Delete();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (this.gvFrom.SelectedRowsCount > 0)
            {
                for (int i = this.gvFrom.RowCount - 1; i >= 0; i--)
                {
                    if (this.gvFrom.IsRowSelected(i))
                    {
                        DataRow dr = this.dtFrom.Rows.Find(this.getPrimaryValue(this.gvFrom.GetDataRow(i)));
                        this.addRow(dr);
                    }
                }
            }
            else
            {
                DataRow focusedDataRow = this.gvFrom.GetFocusedDataRow();
                this.addRow(focusedDataRow);
            }
        }

        private void btMinus_Click(object sender, EventArgs e)
        {
            if (this.gvTo.SelectedRowsCount > 0)
            {
                for (int i = this.gvTo.RowCount - 1; i >= 0; i--)
                {
                    if (this.gvTo.IsRowSelected(i))
                    {
                        DataRow dr = this.dtTo.Rows.Find(this.getPrimaryValue(this.gvTo.GetDataRow(i)));
                        this.delRow(dr);
                    }
                }
            }
            else
            {
                DataRow focusedDataRow = this.gvTo.GetFocusedDataRow();
                this.delRow(focusedDataRow);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if ((this._saveData == null) || this._saveData(this.dtTo))
            {
                this.dtTo_s.Rows.Clear();
                for (int i = 0; i < this.dtTo.Rows.Count; i++)
                {
                    if (this.dtTo.Rows[i].RowState != DataRowState.Deleted)
                    {
                        this.dtTo_s.Rows.Add(this.dtTo.Rows[i].ItemArray);
                    }
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                base.Close();
            }
        }

        private void delRow(DataRow dr)
        {
            DataRow row = this.dtFrom.NewRow();
            for (int i = 0; i < this.dtTo.Columns.Count; i++)
            {
                string columnName = this.dtTo.Columns[i].ColumnName;
                if (this.dtFrom.Columns.Contains(columnName))
                {
                    row[columnName] = dr[columnName];
                }
            }
            this.dtFrom.Rows.Add(row);
            dr.Delete();
        }

        private object[] getPrimaryValue(DataRow dr)
        {
            object[] objArray = new object[this.dtFrom.PrimaryKey.Length];
            for (int i = 0; i < this.dtFrom.PrimaryKey.Length; i++)
            {
                string str = this.dtFrom.PrimaryKey[i].ColumnName.ToUpper();
                for (int j = 0; j < this.dtFrom.Columns.Count; j++)
                {
                    if (this.dtFrom.Columns[j].ColumnName.ToUpper() == str)
                    {
                        objArray[i] = dr[j];
                        break;
                    }
                }
            }
            return objArray;
        }

        protected virtual void SetColumns(string[] column_name, int[] column_width)
        {
            for (int i = 0; i < this.gvFrom.Columns.Count; i++)
            {
                this.gvFrom.Columns[i].Caption = column_name[i];
                this.gvTo.Columns[i].Caption = column_name[i];
                if (column_width[i] == 0)
                {
                    this.gvFrom.Columns[i].Visible = false;
                    this.gvTo.Columns[i].Visible = false;
                }
                else
                {
                    this.gvFrom.Columns[i].Width = column_width[i];
                    this.gvTo.Columns[i].Width = column_width[i];
                }
                this.gvFrom.Columns[i].OptionsColumn.ReadOnly = true;
                this.gvTo.Columns[i].OptionsColumn.ReadOnly = true;
            }
        }

        public delegate bool SaveData(DataTable dt);
    }
}
